/**
 * Created by 鄢鹏权 on 2017/05/01.
 */

function clone(obj) {
    let o;
    if (typeof obj == "object") {
        if (obj === null) {
            o = null;
        } else {
            if (obj instanceof Array) {
                o = [];
                for (let i = 0, len = obj.length; i < len; i++) {
                    o.push(clone(obj[i]));
                }
            } else {
                o = {};
                for (let j in obj) {
                    o[j] = clone(obj[j]);
                }
            }
        }
    } else {
        o = obj;
    }
    return o;
}
function loadJs(url){
    let oHead = document.getElementsByTagName('HEAD').item(0);
    let oScript= document.createElement("script");
    oScript.type = "text/javascript";
    oScript.src=url;
    oHead.appendChild( oScript);
}

function avalonData(obj) {
    if(obj !== undefined && obj !== null){
        let keys = Object.keys(obj);
        for (let i = 0; i < keys.length; i++) {
            if (keys[i].substr(0, 1) != '$')
                if (typeof obj[keys[i]] == 'object') {
                    if (obj[keys[i]] instanceof Array) {
                        this[keys[i]] = [];
                    }
                    else if (obj[keys[i]] instanceof Object) {
                        this[keys[i]] = {};
                    }
                    else { }
                    avalonData.apply(this[keys[i]], [obj[keys[i]]]);
                }
                else {
                    this[keys[i]] = obj[keys[i]];
                }
        }
    }
}
function avalon_data(obj){
    let p ={}
    avalonData.apply(p,[obj]);
    return p;
}
function hereDoc(func) {
    return func.toString().split(/\n/).slice(1, -1).join('\n');
}
/**
 * 获取get参数
 * @param name
 * @returns {any}
 */
function get_query_param(name)
{
    let reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}
/**
 * 跳转内容
 * @param Obj
 * @param Type
 * @param ID
 * @param Dir
 * @param Params
 */
function goto(Obj, Type, ID, Dir='', Params={}) {
    if (Type === void 0) { Type = ''; }
    if (ID === void 0) { ID = 0; }
    if (Dir === void 0) { Dir = ''; }
    if (Params === void 0) { Params = {}; }
    // static s={};
    Config.PageParams = Params;
    if (Obj.slice(0, 1) === '!') {
        //手动传入的
        window.location.hash = Obj;
    }
    else if (Obj.split('/').length > 1) {
        if (Obj.slice(0, 1) != '/') {
            Obj = '/' + Obj;
        }
        window.location.hash = '!' + Obj;
    }
    else if (Obj.length > 0 && Type.length > 0) {
        window.location.hash = '!' + Dir + '/' + Obj + '_' + Type + '/' + ID;
    }
    else if (false === Type) {
        window.location.hash = '!' + Dir + '/' + Obj + '/' + ID;
    }
    else {
        window.location.href = Obj;
    }
}
function menu_click(m:Menu.Item,id:string='0'){
    if(Config._before_menu instanceof Function){
        Config._before_menu(m,id);
    }
    if(m.Sub.length==0)
        goto(m.Obj,m.Type,id);
    if(Config._after_menu instanceof Function){
        Config._after_menu(m,id);
    }
}